using Moq;
using Myotragus.Data.Criteria;
using NHibernate;
using NUnit.Framework;

namespace Myotragus.Data.Tests.Tests.Criteria
{
	[TestFixture]
	public class WriteQueryBuilderFixtures
	{
		[Test]
		public void Save()
		{
			var factory = new Mock<ISessionFactoryAdapter>();
			var session = new Mock<ISession>();
			var transaction = new Mock<ITransaction>();

			var o1 = new object();
			var o2 = new object();
			var o3 = new object();

			factory.Setup(m => m.OpenSession()).Returns(session.Object);

			transaction.Setup(m => m.Commit());

			session.Setup(m => m.BeginTransaction()).Returns(transaction.Object);

			session.Setup(m => m.Save(o1));
			session.Setup(m => m.Save(o2));
			session.Setup(m => m.Save(o3));

			session.Setup(m => m.Flush());

			var writer = new WriteQueryBuilder(factory.Object);
			writer.Save(new[] { o1, o2, o3 });

			session.VerifyAll();
			factory.VerifyAll();
			transaction.VerifyAll();
		}

		[Test]
		public void Delete()
		{
			var factory = new Mock<ISessionFactoryAdapter>();
			var session = new Mock<ISession>();
			var transaction = new Mock<ITransaction>();

			var o1 = new object();
			var o2 = new object();
			var o3 = new object();

			factory.Setup(m => m.OpenSession()).Returns(session.Object);

			transaction.Setup(m => m.Commit());

			session.Setup(m => m.BeginTransaction()).Returns(transaction.Object);

			session.Setup(m => m.Delete(o1));
			session.Setup(m => m.Delete(o2));
			session.Setup(m => m.Delete(o3));

			session.Setup(m => m.Flush());

			var writer = new WriteQueryBuilder(factory.Object);
			writer.Delete(new[] { o1, o2, o3 });

			session.VerifyAll();
			factory.VerifyAll();
			transaction.VerifyAll();
		}
	}
}